/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.az.ducbt.cliphot;

import java.io.IOException;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

/**
 *
 * @author Administrator
 */
public class ReadJson {

    JsonParser jsonParser = new JsonParser();
    Constant ct = new Constant();
    CryptText crt = new CryptText();

    public ReadJson() {
    }

    public DataInfo readDataJsonInfo(String url) throws IOException {
        DataInfo dataInfo = new DataInfo();
        try {
            JSONObject jObj = jsonParser.getJsonDataFromUrl(url);
            if (jObj == null) {
                return null;
            }
            JSONObject dataJO = jObj.getJSONObject(ct.DATA_INFO);
            dataInfo.setSoTrangCMN(dataJO.getInt(ct.SO_TRANG_CLIP_MOI_NHAT));
            dataInfo.setSoTrangCH(dataJO.getInt(ct.SO_TRANG_CLIP_HOT));
            dataInfo.setSoTrangCXN(dataJO.getInt(ct.SO_TRANG_CLIP_XEM_NHIEU));
            dataInfo.setHostUrl(crt.decode(dataJO.getString(ct.HOST_URL)));
            dataInfo.setSmsID(crt.decode(dataJO.getString(ct.SMS_ID)));
            dataInfo.setHeadSms(crt.decode(dataJO.getString(ct.HEAD_SMS)));
            dataInfo.setMaxSend(dataJO.getInt(ct.MAX_SEND));
            dataInfo.setMaxSmsID(dataJO.getInt(ct.MAX_SMS_ID));

            //System.out.println("DataInfo:\n New: " + dataInfo.getSoTrangCMN() + " Hot: " + dataInfo.getSoTrangCH() + " View: " + dataInfo.getSoTrangCXN() + " Video Url: " + dataInfo.getHostUrl() + " Sms ID: " + dataInfo.getSmsID() + " Max send: " + dataInfo.getMaxSend() + " Max sms id: " + dataInfo.getMaxSmsID());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return dataInfo;
    }

    public ClipItem[] readDataJsonClip(String url) throws IOException {
        JSONArray jAry = null;
        ClipItem[] listClip = new ClipItem[10];
        try {
            JSONObject jObj = jsonParser.getJsonDataFromUrl(url);
            if (jObj == null) {
                return null;
            }
            jAry = jObj.getJSONArray(ct.CLIPS);
            for (int i = 0; i < jAry.length(); i++) {
                JSONObject t = jAry.getJSONObject(i);
                listClip[i] = new ClipItem();
                listClip[i].setClipID(t.getString(ct.CLIP_ID));
                listClip[i].setClipName(t.getString(ct.CLIP_NAME));
                listClip[i].setImageUrl(t.getString(ct.IMAGE_URL));
                listClip[i].setClipUrl(t.getString(ct.CLIP_URL));
                //clipItem.setCountView(t.getInt(ct.COUNT_VIEW));
                //System.out.println("Clip name: " + listClip[i].getClipName() + " clip ID: " + listClip[i].getClipID());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return listClip;
    }

    public HeadNumber[] readDataHeadNumber(String url) throws IOException {
        JSONArray jAry = null;
        HeadNumber[] listNumber = new HeadNumber[8];
        try {
            JSONObject jObj = jsonParser.getJsonDataFromUrl(url);
            if (jObj == null) {
                return null;
            }
            jAry = jObj.getJSONArray(ct.HEAD_NUMBER);
            for (int i = 0; i < jAry.length(); i++) {
                JSONObject t = jAry.getJSONObject(i);
                listNumber[i] = new HeadNumber();
                listNumber[i].setNumber(t.getInt(ct.NUMBER));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return listNumber;
    }
}
